
Page({
    data: {
        topic:[{"id":20,"name":"全部","type":0,"sort":1},{"id":10,"name":"求职","type":10,"sort":2},{"id":11,"name":"招聘","type":20,"sort":3},{"id":12,"name":"求购","type":30,"sort":4},{"id":17,"name":"租赁","type":80,"sort":9},{"id":19,"name":"办证","type":100,"sort":11}],
        arr:[1,2,3],
        header:{},
        userInfo:{},
        bbsArr:[],
        label:'',
        showLogin:false,
        current:1,
        pagesize:10,
        toTop:false,
        moreText:'加载更多...',
        totalPage:1


    },
    turnCity:function(){
      wx.navigateTo({
        url: '/pages/cityChoose/webView?type=3'
      })
    },
    onPageScroll:function(e){
    console.log(e.scrollTop);//{scrollTop:99}
       if (e.scrollTop>500) {
        this.setData({
          toTop:true
        })
       }else{
          this.setData({
          toTop:false
        })
       }
    },
     onReachBottom:function(){
        console.log('加载更多')
        this.setData({
          current:this.data.current+1
        })

        if (this.data.current<=this.data.totalPage) {
           this.getbbsById(this.data.selectType);
        }else{
          this.setData({
            moreText:'没有更多了'
          })
        }


       
    },
    onPullDownRefresh:function () {
      this.setData({
        current:1
      })

      console.log('刷新')
      wx.stopPullDownRefresh();
      this.getbbsById(this.data.selectType);

    },
   
     showLoading:function () {
      console.log('123231')
      this.setData({
        showLogin:true
      })
    },
    onCannel:function(){
     this.setData({
        showLogin:false
      })
    },
   
    onLoginSuccess:function () {
      this.setData({
        showLogin:false
      })
    },
   
    //获取热门话题
    gethotTopic:function(){
       //  var _self=this;
       //   wx.request({
       //      url: getApp().globalData.baseUrl + '/wx/articles/hotTopic',
       //      method:"GET",
       //      header: this.data.header,
       //      success: function(res) {
       //       console.log(res.data)
       //      if (res.data.code==200) {
       //        var json={name:'全部',type:0};
       //        var arr=[];
       //        arr.push(json);
       //       for(var i=0;i<res.data.obj.length;i++){
       //          res.data.obj[i].select='nomal';
       //          arr.push(res.data.obj[i]);
       //       }
       //       arr[0].select='select';
       //        _self.setData({
       //          topic:arr,
       //          label:res.data.obj[0].name
       //         })  
       //      _self.getbbsById(arr[0].type);            
       //       }
           
       //   }
       // })
      for(var i=0;i< this.data.topic.length;i++){
         this.data.topic[i].select='nomal';
      }
 
      this.data.topic[0].select='select';
      this.setData({
         topic: this.data.topic,
         selectType:this.data.topic[0].type
      })
      this.getbbsById(this.data.topic[0].type);   
      wx.stopPullDownRefresh();

    },
    //切换热门话题
    getBbs:function(e){
       var id=e.target.dataset.code;
       console.log("******");
       console.log(id);
      
       this.getbbsById(id);
       var arr=this.data.topic;
       var name='';
       for(var j = 0, length2 = arr.length; j < length2; j++){
           if (id==arr[j].type) {
            arr[j].select='select';
            name=arr[j].name;
           }else{
            arr[j].select='nomal';
           }
           
       }
       console.log(123)
       this.setData({
          topic:arr,
          label:name,
          selectType:id,
          current:1,
          bbsArr:[]
       }) 
    },

    //获取bbs列表
    getbbsById:function(id){
      var url='';
       if(id==0){
         url='/wx/articles/getList?status=20'+'&current='+this.data.current+'&pagesize='+this.data.pagesize;
        
       }else{
         if (this.data.city!='全国') {
           url='/wx/articles/getList?status=20&hotTopicType='+id+'&city='+this.data.city+'&province='+this.data.province+'&current='+this.data.current+'&pagesize='+this.data.pagesize
         }else{
           url='/wx/articles/getList?status=20&hotTopicType='+id+'&current='+this.data.current+'&pagesize='+this.data.pagesize
         }
        
       }
       if (this.data.city!='全国'&&id==0) {
         url+='&province='+this.data.province+'&city='+this.data.city;
       }
        var _self=this;
         wx.request({
            url: getApp().globalData.baseUrl + url,
            method:"GET",
            header: this.data.header,
            success: function(res) {
             if (res.data.code!=200) {
              return
             }
             _self.setData({
              bbsArr: _self.data.bbsArr.concat(res.data.obj.list),
              totalPage:res.data.obj.pages
             })
         
           
         }
       })

    },
    onMyEvent:function(province){
      console.log(province.detail)
      this.setData({
        province:province.detail.split(',')[0],
        city:province.detail.split(',')[1],
        bbsArr:[]
      })
      this.getbbsById(this.data.selectType);
     
    },
     onShow:function(){

       var chooseProvince= wx.getStorageSync('chooseProvince3');
       var chooseCity2=wx.getStorageSync('chooseCity3');
       if (chooseCity2==this.data.city) {
        return
       }
      
        if (chooseProvince) {
          this.setData({
            city:wx.getStorageSync('chooseCity3')||'全国',
            item:wx.getStorageSync('chooseCity3')||'全国',
            province:wx.getStorageSync('chooseProvince3')||'全国'
          })
        }
      this.setData({
        bbsArr:[]
      })
      this.gethotTopic();
    },
    onLoad:function(){
       var address= wx.getStorageSync('address');
       var chooseProvince= wx.getStorageSync('chooseProvince3');
       if (!chooseProvince) {
          wx.setStorageSync('chooseProvince3','全国');
          wx.setStorageSync('chooseCity3','全国');
       }
     var address= wx.getStorageSync('address');
        this.setData({
            address:address,
            city:'全国',
            item:'全国',
            province:"全国"
        })
         console.log('社区')
        getApp().getUserInfo().then(res=>{
      var header={
             'openid':res.openid, //公用请求头
             'content-type': 'application/json'
         }
       this.setData({
        userInfo:res,
        header:header,
        bbsArr:[],
        current:1
       })  

       this.gethotTopic();
     })
       
    },
    turnDetail:function(e){
       console.log(e)
        var userInfo= wx.getStorageSync('userInfo');
       if (!userInfo.nickName) {
         this.setData({
          showLogin:true
         })
         return
       }
    	wx.navigateTo({
            url: '/pages/communityDetail/communityDetail?id='+e.currentTarget.dataset.code.id
        })
    },
    turnPage:function(){
         wx.pageScrollTo({
           scrollTop: 1
        })
    },
    //点赞
    dianzanM:function(e){
         var _self=this;
        var id=e.target.dataset.code;
          console.log(e)
         wx.request({
            url: getApp().globalData.baseUrl + '/wx/articles/browse/save',
            method:"POST",
            data:{
              openid:this.data.userInfo.openid,
              articlesId:id,
              type:1
            },
            header: this.data.header,
            success: function(res) {
             console.log(res.data.code)
             if (res.data.code==200) {
               var arr=_self.data.bbsArr;
               var name='';
               for(var j = 0, length2 = arr.length; j < length2; j++){
                   if (id==arr[j].id) {
                    arr[j].isLikes=1;
                    arr[j].likesNum+=1;
                    
                   } 
               }
               console.log(arr)
               _self.setData({
                  bbsArr:arr
                  
               }) 

             }         
           
         }
       })

    },
    //取消点赞
    dianzanF:function(e){
         var _self=this;
        var id=e.target.dataset.code;
          console.log(e)
         wx.request({
            url: getApp().globalData.baseUrl + '/wx/articles/browse/delete?openid='+this.data.userInfo.openid+'&articlesId='+id,
            method:"DELETE",
            body:{
              openid:this.data.userInfo.openid,
              articlesId:id,
            },
            header: this.data.header,
            success: function(res) {
             console.log(res.data.code)
             if (res.data.code==200) {
               var arr=_self.data.bbsArr;
               var name='';
               for(var j = 0, length2 = arr.length; j < length2; j++){
                   if (id==arr[j].id) {
                    arr[j].isLikes=0;
                    arr[j].likesNum-=1;
                    
                   } 
               }
               console.log(arr)
               _self.setData({
                  bbsArr:arr
                  
               }) 

             }         
           
         }
       })

    },
     onShareAppMessage:function(){
         return {
              title: '建筑工地求职招聘免费平台',
              imageUrl: '' // 图片 URL
          }
    },
    onShareTimeline:function(){
         return {
              title: '建筑工地求职招聘免费平台',
              imageUrl: '' // 图片 URL
          }
    },
});